package figure8.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

import figure8.Controller;
import figure8.Seq;

/**
 * This class is a menu bar and sets up all the buttons for the menu bar as well
 * as making listeners for all the buttons that forward commands to the
 * controller.
 */
public class seqMenuBar extends JMenuBar {
    private Controller controller;
    
	// This is a bit of a hack to make storing all the different buttons nicer,
    // this way there isn't a member variable for every single button. Might be
    // worth changing this near the end when the GUI is finalised and we can see
    // if it's necessary.
    private Map<MenuItems, JMenuItem> menuItems;

    public seqMenuBar(final Seq sequencer) {
        super();
        
        this.controller = controller;
        this.menuItems = new HashMap<MenuItems, JMenuItem>();
        
        // Create the project menu
        JMenu project = new JMenu("<html><u>P</u>roject</html>");
        project.setMnemonic(KeyEvent.VK_P);
        
        // Create all the project menu items and add listeners to them which
        // forward the commands to the controller.
        this.menuItems.put(MenuItems.NEW, new JMenuItem("NEW"));
        this.menuItems.get(MenuItems.NEW).setMnemonic(KeyEvent.VK_O);
        this.menuItems.get(MenuItems.NEW).setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_O, ActionEvent.ALT_MASK));
        this.menuItems.get(MenuItems.NEW).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                    	
                    }
                });
        
        this.menuItems.put(MenuItems.OPEN, new JMenuItem("Open"));
        this.menuItems.get(MenuItems.OPEN).setMnemonic(KeyEvent.VK_O);
        this.menuItems.get(MenuItems.OPEN).setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_O, ActionEvent.ALT_MASK));
        this.menuItems.get(MenuItems.OPEN).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                    	sequencer.open();
                    }
                });
        
        this.menuItems.put(MenuItems.SAVE, new JMenuItem("Save"));
        this.menuItems.get(MenuItems.SAVE).setMnemonic(KeyEvent.VK_S);
        this.menuItems.get(MenuItems.SAVE).setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_S, ActionEvent.ALT_MASK));
        this.menuItems.get(MenuItems.SAVE).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                    	sequencer.save();
                    }
                });
        
        this.menuItems.put(MenuItems.FIGURE8, new JMenuItem("Figure8"));
        this.menuItems.get(MenuItems.FIGURE8).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                    	sequencer.close();
                    }
                });
        project.add(this.menuItems.get(MenuItems.OPEN));
        project.add(this.menuItems.get(MenuItems.SAVE));
        project.add(this.menuItems.get(MenuItems.FIGURE8));
        
        // Add the project menu to the menu bar.
        this.add(project);
        
        // Add a place holder settings menu.
        
        // Create a help menu and add place holder items and separators to it.
        JMenu help = new JMenu("<html><u>H</u>elp</html>");
        help.setMnemonic(KeyEvent.VK_H);
        JMenuItem userGuide = new JMenuItem("User Guide");
        userGuide.setMnemonic(KeyEvent.VK_U);
        userGuide.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
        userGuide.addActionListener(
                new ActionListener() {
                	@Override
                    public void actionPerformed(ActionEvent e) {
                        controller.userGuide();
                    }
                });
        help.add(userGuide);
        JMenuItem contactUs = new JMenuItem("Contact Us");
        contactUs.setMnemonic(KeyEvent.VK_C);
        contactUs.addActionListener(
                new ActionListener() {
                	@Override
                    public void actionPerformed(ActionEvent e) {
                        controller.contactUs();
                    }
                });
        help.add(contactUs);
        help.add(new JSeparator());
        JMenuItem about = new JMenuItem("About");
        about.setMnemonic(KeyEvent.VK_A);
        about.addActionListener(
                new ActionListener() {
                	@Override
                    public void actionPerformed(ActionEvent e) {
                        controller.about();
                    }
                });
        help.add(about);
        
        // Add the help menu to the menu bar.
        this.add(help);
    }
}
